#!/usr/bin/python -S
"""
xmap_test.py: Tests for xmap.py
"""

__author__ = 'Andy Chu'


import os
import Queue
import subprocess
import sys
import unittest

from torn import base
from torn import child_process
from torn import ioloop
from torn import log as torn_log

import xmap_lib  # module under test


class ProcessTest(unittest.TestCase):

  def testProcess(self):
    torn_log.install_default_logger()
    # FUCK: easy to forget this
    child_process.RegisterSignalHandlers()

    print 'unit test PID:', os.getpid()

    c = child_process.ChildProcess(['./sleep_.py'],
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE)
    def echo(value):
      print 'VALUE', value

    def close():
      print 'CLOSED'

    def exit(exit_code, signum):
      print 'EIXT!!!', exit_code, signum
      ioloop.instance().stop()

    c.on('exit', exit)

    c.stdout.on('data', echo)
    c.stdout.on('close', close)

    c.stdin.write('1:1#')

    print '*** CLOSING stdin'

    # let it know this is the end of the stream; it should in turn close stdout
    c.stdin.close()

    ioloop.instance().start()


if __name__ == '__main__':
  unittest.main()
